/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.securityrat.entities_temp;

import java.io.Serializable;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

/**
 *
 * @author p.vivoli
 */
@Entity
@Table(name = "system")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "System.findAll", query = "SELECT s FROM System s"),
    @NamedQuery(name = "System.findById", query = "SELECT s FROM System s WHERE s.id = :id"),
    @NamedQuery(name = "System.findByNome", query = "SELECT s FROM System s WHERE s.nome = :nome"),
    @NamedQuery(name = "System.findByStato", query = "SELECT s FROM System s WHERE s.stato = :stato"),
    @NamedQuery(name = "System.findByOwner", query = "SELECT s FROM System s WHERE s.owner = :owner"),
    @NamedQuery(name = "System.findByGestSys", query = "SELECT s FROM System s WHERE s.gestSys = :gestSys"),
    @NamedQuery(name = "System.findByGestApp", query = "SELECT s FROM System s WHERE s.gestApp = :gestApp"),
    @NamedQuery(name = "System.findByGestSvi", query = "SELECT s FROM System s WHERE s.gestSvi = :gestSvi"),
    @NamedQuery(name = "System.findByImpattiR", query = "SELECT s FROM System s WHERE s.impattiR = :impattiR"),
    @NamedQuery(name = "System.findByImpattiIPoi", query = "SELECT s FROM System s WHERE s.impattiIPoi = :impattiIPoi"),
    @NamedQuery(name = "System.findByImpattiIPfd", query = "SELECT s FROM System s WHERE s.impattiIPfd = :impattiIPfd"),
    @NamedQuery(name = "System.findByImpattiDPcc", query = "SELECT s FROM System s WHERE s.impattiDPcc = :impattiDPcc"),
    @NamedQuery(name = "System.findByImpattiDRda", query = "SELECT s FROM System s WHERE s.impattiDRda = :impattiDRda"),
    @NamedQuery(name = "System.findByImpattiLegal", query = "SELECT s FROM System s WHERE s.impattiLegal = :impattiLegal"),
    @NamedQuery(name = "System.findByImpattiProcesso", query = "SELECT s FROM System s WHERE s.impattiProcesso = :impattiProcesso"),
    @NamedQuery(name = "System.findByEsposizione", query = "SELECT s FROM System s WHERE s.esposizione = :esposizione"),
    @NamedQuery(name = "System.findByBacinoUtenti", query = "SELECT s FROM System s WHERE s.bacinoUtenti = :bacinoUtenti"),
    @NamedQuery(name = "System.findByInherentRisk", query = "SELECT s FROM System s WHERE s.inherentRisk = :inherentRisk"),
    @NamedQuery(name = "System.findByResidualRisk", query = "SELECT s FROM System s WHERE s.residualRisk = :residualRisk")})
public class System implements Serializable {
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "system")
    private Collection<RelSp> relSpCollection;
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Size(max = 255)
    @Column(name = "nome")
    private String nome;
    @Lob
    @Size(max = 65535)
    @Column(name = "descrizione")
    private String descrizione;
    @Lob
    @Size(max = 65535)
    @Column(name = "note")
    private String note;
    @Size(max = 50)
    @Column(name = "stato")
    private String stato;
    @Size(max = 255)
    @Column(name = "owner")
    private String owner;
    @Size(max = 255)
    @Column(name = "gest_sys")
    private String gestSys;
    @Size(max = 255)
    @Column(name = "gest_app")
    private String gestApp;
    @Size(max = 255)
    @Column(name = "gest_svi")
    private String gestSvi;
    @Size(max = 255)
    @Column(name = "impatti_r")
    private String impattiR;
    @Size(max = 255)
    @Column(name = "impatti_i_poi")
    private String impattiIPoi;
    @Size(max = 255)
    @Column(name = "impatti_i_pfd")
    private String impattiIPfd;
    @Size(max = 255)
    @Column(name = "impatti_d_pcc")
    private String impattiDPcc;
    @Size(max = 255)
    @Column(name = "impatti_d_rda")
    private String impattiDRda;
    @Size(max = 255)
    @Column(name = "impatti_legal")
    private String impattiLegal;
    @Size(max = 50)
    @Column(name = "impatti_processo")
    private String impattiProcesso;
    @Size(max = 50)
    @Column(name = "esposizione")
    private String esposizione;
    @Size(max = 50)
    @Column(name = "bacino_utenti")
    private String bacinoUtenti;
    @Size(max = 10)
    @Column(name = "inherent_risk")
    private String inherentRisk;
    @Size(max = 10)
    @Column(name = "residual_risk")
    private String residualRisk;
    @JoinColumn(name = "id_datatype", referencedColumnName = "id")
    @ManyToOne
    private Datatype idDatatype;

    public System() {
    }

    public System(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getDescrizione() {
        return descrizione;
    }

    public void setDescrizione(String descrizione) {
        this.descrizione = descrizione;
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }

    public String getStato() {
        return stato;
    }

    public void setStato(String stato) {
        this.stato = stato;
    }

    public String getOwner() {
        return owner;
    }

    public void setOwner(String owner) {
        this.owner = owner;
    }

    public String getGestSys() {
        return gestSys;
    }

    public void setGestSys(String gestSys) {
        this.gestSys = gestSys;
    }

    public String getGestApp() {
        return gestApp;
    }

    public void setGestApp(String gestApp) {
        this.gestApp = gestApp;
    }

    public String getGestSvi() {
        return gestSvi;
    }

    public void setGestSvi(String gestSvi) {
        this.gestSvi = gestSvi;
    }

    public String getImpattiR() {
        return impattiR;
    }

    public void setImpattiR(String impattiR) {
        this.impattiR = impattiR;
    }

    public String getImpattiIPoi() {
        return impattiIPoi;
    }

    public void setImpattiIPoi(String impattiIPoi) {
        this.impattiIPoi = impattiIPoi;
    }

    public String getImpattiIPfd() {
        return impattiIPfd;
    }

    public void setImpattiIPfd(String impattiIPfd) {
        this.impattiIPfd = impattiIPfd;
    }

    public String getImpattiDPcc() {
        return impattiDPcc;
    }

    public void setImpattiDPcc(String impattiDPcc) {
        this.impattiDPcc = impattiDPcc;
    }

    public String getImpattiDRda() {
        return impattiDRda;
    }

    public void setImpattiDRda(String impattiDRda) {
        this.impattiDRda = impattiDRda;
    }

    public String getImpattiLegal() {
        return impattiLegal;
    }

    public void setImpattiLegal(String impattiLegal) {
        this.impattiLegal = impattiLegal;
    }

    public String getImpattiProcesso() {
        return impattiProcesso;
    }

    public void setImpattiProcesso(String impattiProcesso) {
        this.impattiProcesso = impattiProcesso;
    }

    public String getEsposizione() {
        return esposizione;
    }

    public void setEsposizione(String esposizione) {
        this.esposizione = esposizione;
    }

    public String getBacinoUtenti() {
        return bacinoUtenti;
    }

    public void setBacinoUtenti(String bacinoUtenti) {
        this.bacinoUtenti = bacinoUtenti;
    }

    public String getInherentRisk() {
        return inherentRisk;
    }

    public void setInherentRisk(String inherentRisk) {
        this.inherentRisk = inherentRisk;
    }

    public String getResidualRisk() {
        return residualRisk;
    }

    public void setResidualRisk(String residualRisk) {
        this.residualRisk = residualRisk;
    }

    public Datatype getIdDatatype() {
        return idDatatype;
    }

    public void setIdDatatype(Datatype idDatatype) {
        this.idDatatype = idDatatype;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof System)) {
            return false;
        }
        System other = (System) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.securityrat.entities.System[ id=" + id + " ]";
    }

    @XmlTransient
    public Collection<RelSp> getRelSpCollection() {
        return relSpCollection;
    }

    public void setRelSpCollection(Collection<RelSp> relSpCollection) {
        this.relSpCollection = relSpCollection;
    }
    
    
    // --------------------------------
    
    public JSONObject getJsonObj () {
        
        JSONObject systemJson = new JSONObject();
        JSONArray perimetriJson = null;
                
        
        systemJson.put("id",this.getId().toString());           
        systemJson.put("nome",this.getNome());
        systemJson.put("descrizione", this.getDescrizione());
        systemJson.put("stato", this.getStato());
        systemJson.put("datatype", this.getIdDatatype().getDescrizione());
        
        perimetriJson = new JSONArray();
                
        /*
         * Ciclo sui perimetri associati al sistema e 
         * li inserisco in lista perimetri
         */
        Collection<RelSp> perim_sys = this.getRelSpCollection();
        for (RelSp rel_sp : perim_sys) {
            Perimetro p = rel_sp.getPerimetro();
            perimetriJson.add(p.getDescrizione());
        }

        systemJson.put("perimetri_lista",perimetriJson);
        
        return systemJson;
        
    }
    
    
}
